package com.rfid.a252robot.admin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.rfid.a252robot.R;
import com.rfid.a252robot.admin.adapter.ChargeAdapter;
import com.rfid.a252robot.admin.bean.Address;
import com.rfid.a252robot.base.Listener;
import com.rfid.a252robot.event.MapInfoEvent;
import com.rfid.common.log.LogUtils;
import com.rfid.mqtt.MqttManager;
import com.rfid.mqtt.bean.MapInfo;
import com.rfid.util.ACache;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChargePointSelectActivity extends Activity {

    @BindView(R.id.recyclerView)
    RecyclerView mSelectRecyclerView;
    ChargeAdapter mChargeAdapter;
    List<Address> mData = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charge_select_layout);
        ButterKnife.bind(this);
        mChargeAdapter = new ChargeAdapter(mData);
        mSelectRecyclerView.setAdapter(mChargeAdapter);
        mSelectRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mSelectRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        mChargeAdapter.setListener(new Listener<Address>() {
            @Override
            public void onClick(Address address) {
                Intent intent = new Intent();
                intent.putExtra("mapCode",address.getMapCode());
                intent.putExtra("pointName",address.getPointName());
                setResult(100,intent);
                finish();
            }
        });
        EventBus.getDefault().register(this);
        MqttManager.INSTANCE.getMapInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMapInfo(MapInfoEvent event) {
        LogUtils.v("onMapInfo = " + event.getInfo());
        MapInfo info = event.getInfo();
        List<MapInfo.DataBeanX.DataBean> locations = info.getData().getData();
        List<Address> addresses = new ArrayList<>();
        for (MapInfo.DataBeanX.DataBean bean : locations) {
            if (bean.getActive() != 1) {
                continue;
            }
            List<MapInfo.DataBeanX.DataBean.BizPointsBean>  points = bean.getBizPoints();
            for (MapInfo.DataBeanX.DataBean.BizPointsBean point : points) {
                Address address = new Address();
                address.setBizPointId(point.getBizPointId());
                address.setPointName(point.getPointName());
                address.setMapCode(point.getMapCode());
                addresses.add(address);
            }
        }
        mData.clear();
        mData.addAll(addresses);
        mChargeAdapter.notifyDataSetChanged();
    }



}
